package com.gimi.cloud.jimi.common.enums;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.map.LinkedMap;

/**
 * 公共的枚举类
 *
 * @author yy
 * @version 1.0
 * @date 2015年11月25日
 */
public class CommonEnume {
    public static Map<String, Class> enumsmap;

    static {
        enumsmap = new HashMap<String, Class>();
        //初始化枚举map
        enumsmap.put("orderSource", CommonEnume.OrderSource.class);//订单来源
        enumsmap.put("orderStatus", CommonEnume.OrderStatus.class);//订单状态
        enumsmap.put("payType", CommonEnume.PayType.class);//订单支付方式
        enumsmap.put("orderLogisticsStatus", CommonEnume.OrderLogisticsStatus.class);//订单发货状态
        enumsmap.put("logisticsStatus", CommonEnume.LogisticsStatus.class);//订单物流状态
        enumsmap.put("adStatus", CommonEnume.AdStatus.class);//App广告状态
        enumsmap.put("payStatus", CommonEnume.PayStatus.class);//支付状态
        enumsmap.put("expenseItem", CommonEnume.ExpenseItem.class);//费用项定义

    }

    public interface BaseEnum {
        public abstract String getValue();
    }

    /**
     * 转换枚举为Map
     *
     * @param clz
     * @return
     */
    public static Map ConvertEnumMap(Class clz) {
        Map keymap = new LinkedMap();
        for (Object obj : clz.getEnumConstants()) {
            if (obj instanceof BaseEnum) {
                BaseEnum b = (BaseEnum) obj;
                if (!keymap.containsKey(obj.toString().toUpperCase()))
                    keymap.put(obj.toString().toUpperCase(), b.getValue());
            }
        }
        return keymap;
    }

    /**
     * 根据key得到枚举列表
     *
     * @param key
     * @return
     */
    public static Map getcodelist(String key) {
        if (enumsmap != null && enumsmap.containsKey(key.toLowerCase())) {
            return ConvertEnumMap(enumsmap.get(key.toLowerCase()));
        }
        return new HashMap();
    }

    public static List getCodeList1(String key) {
        if (enumsmap != null && enumsmap.containsKey(key.toLowerCase())) {
            Map map = ConvertEnumMap(enumsmap.get(key.toLowerCase()));

            List<Map<String, String>> array = new ArrayList<Map<String, String>>();
            Set<Map.Entry<String, String>> entryseSet = map.entrySet();
            for (Map.Entry<String, String> entry : entryseSet) {
                Map<String, String> node = new HashMap<String, String>();
                node.put("code", entry.getKey());
                node.put("value", entry.getValue());
                array.add(node);
            }
            return array;
        }
        return new ArrayList();
    }

    public static List getCodeList1(String key, String deleteKey) {
        Map<String, String> delKeysMap = new HashMap<String, String>();
        for (String delKey : deleteKey.split(",")) {
            delKeysMap.put(delKey, delKey);
        }
        if (enumsmap != null && enumsmap.containsKey(key.toLowerCase())) {
            Map map = ConvertEnumMap(enumsmap.get(key.toLowerCase()));
            List<Map<String, String>> array = new ArrayList<Map<String, String>>();
            Set<Map.Entry<String, String>> entryseSet = map.entrySet();
            for (Map.Entry<String, String> entry : entryseSet) {
                if (!delKeysMap.containsKey(entry.getKey())) {
                    Map<String, String> node = new HashMap<String, String>();
                    node.put("code", entry.getKey());
                    node.put("value", entry.getValue());
                    array.add(node);
                }
            }
            return array;
        }
        return new ArrayList();
    }

    public static enum OrderSource implements BaseEnum {
        app("app来源"),//app来源
        wechat("微信"),//微信
        other("其他");//其他
        private final String value;

        OrderSource(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //订单状态(A01已提交待支付,A02支付成功待收货,A03已收货,A09取消;L01已提交审核中,L02审核成功待支付,L03审核拒绝,L04支付成功待收货,,L05已收货,L09取消
    public static enum OrderStatus implements BaseEnum {
        A01("已提交待支付"),
        A02("支付成功待收货"),
        A03("已收货"),
        A09("取消"),
        L01("已提交审核中"),
        L02("审核成功待支付"),
        L03("审核拒绝"),
        L04("支付成功待收货"),
        L05("已收货"),
        L09("取消");
        private final String value;

        OrderStatus(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //订单类型(ordinary普通，lease租赁）
    public static enum OrderType implements BaseEnum {
        lease("租赁"),
        ordinary("普通");
        private final String value;

        OrderType(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //支付方式(lease租赁,stages分期付款,online在线付款,delivery货到付款) 订单类型为租赁支付方式为租赁
    public static enum PayType implements BaseEnum {
        lease("租赁"),
        stages("分期付款"),
        online("在线付款"),
        delivery("货到付款");
        private final String value;

        PayType(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //发货状态(yes已发货,no未发货,ing发货中)
    public static enum OrderLogisticsStatus implements BaseEnum {
        yes("已发货"),
        no("未发货"),
        ing("发货中");
        private final String value;

        OrderLogisticsStatus(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }
    //day按天,week按周,month按月,year按年
    public static enum RentType implements BaseEnum {
        day("按天"),
        week("按周"),
        month("按月"),
        year("按年");
        private final String value;

        RentType(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //广告状态,a:未审核,b:审核中,c:待发布,d:已发布,e:已撤下
    public static enum AdStatus implements BaseEnum {
        a("未审核"),//未审核
        b("审核中"),//审核中
        c("待发布"),//待发布
        d("已发布"),//已发布
        e("已撤下");//已撤下
        private final String value;

        AdStatus(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //支付状态(pay 已支付,nopay 未支付)
    public static enum PayStatus implements BaseEnum {
        pay("已支付"),
        nopay("未支付");
        private final String value;

        PayStatus(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //物流状态(transport运输中，distribution配送中，wait待收货，get已收货，finish完成)
    public static enum LogisticsStatus implements BaseEnum {
        transport("运输中"),
        distribution("配送中"),
        wait("待收货"),
        get("已收货"),
        finish("完成");
        private final String value;

        LogisticsStatus(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    //配送方式(express快递,self自取)
    public static enum DistributionType implements BaseEnum{
        express("快递"),
        self("自取");

        private final String value;

        DistributionType(String value) {
            this.value = value;
        }
        @Override
        public String getValue() {
            return value;
        }
    }

    //费用项定义
    public static enum ExpenseItem implements BaseEnum {
        LOAN("放款"),
        IPRINCIPAL("本金"),
        IINTEREST("利息"),
        ISERVICE("服务费"),
        ITECHSERVICE("技术服务费"),
        IFAILUREFEE("扣款失败手续费"),
        ILATEPENALTY("逾期罚息"),
        ILATEPENALTY_C("咨询公司逾期罚息"),
        IADVISORY("咨询费"),
        IOUTSOURCE("外包佣金"),
        IBEFORREPAYMENT("提前还款违约金"),
        IBEFORREPAYMENT_C("咨询公司提前还款违约金"),
        ILATEREPAYMENT("延期费"),
        ILATEREPAYMENT_C("咨询公司延期费"),
        IADVANCE("预收"),
        IADVANCE_C("咨询公司预收"),
        ILATEMANAGE("逾期账户管理费"),
        RETURNGOODS("退货手续费");

        private final String value;

        ExpenseItem(String value) {
            this.value = value;
        }

        @Override
        public String getValue() {
            return value;
        }
    }

    public static void main(String[] agrs) {
        System.out.println(ConvertEnumMap(OrderStatus.class).get("L03"));

        System.out.println(ConvertEnumMap(OrderStatus.class));
        System.out.println(getcodelist("orderstatus"));

        System.out.println(OrderStatus.A01);
        System.out.println(OrderStatus.A01.value);

        System.out.println(CommonEnume.AdStatus.valueOf("d").getValue());
        System.out.println(CommonEnume.AdStatus.valueOf("d").toString());
        System.out.println(CommonEnume.AdStatus.valueOf("d"));
        System.out.println(ExpenseItem.valueOf("LOAN"));
        System.out.println(ExpenseItem.LOAN.getValue());
    }

}